package com.lagace.spark.twitter;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.border.BevelBorder;

import winterwell.jtwitter.Twitter;

public class TweetPanel extends JPanel{
	private static final long serialVersionUID = 1L;
	private Twitter t;
	private JButton update;
	private JTextPane ta;
	
	public TweetPanel(final Twitter t){
		this.t = t;
		update = new JButton("Update");
		update.addActionListener(createUpdateAction());
		ta = new JTextPane();
		ta.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		
		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0; // first column
		gbc.gridy = 0; // first row
		gbc.gridwidth = 1; // occupies only one column
		gbc.gridheight = 1; // occupies only one row
		gbc.weightx = 100;
		gbc.weighty = 1; 
		gbc.fill = GridBagConstraints.BOTH; 
		gbc.anchor = GridBagConstraints.WEST;
		gbl.setConstraints(ta, gbc);
		
		gbc.weightx = 1;
		gbc.gridx = 1; // second column
		gbc.fill = GridBagConstraints.VERTICAL;
		gbc.anchor = GridBagConstraints.EAST;
		gbl.setConstraints(update, gbc);
		
		this.setLayout(gbl);
		this.add(ta);
		this.add(update);
	}
	
	protected void update() {
		t.updateStatus(ta.getText());
		ta.setText(null);
	}

	private ActionListener createUpdateAction(){
		return new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == update && ta.getText().trim().length() > 0)
					update();
			}
		};
	}
}
